import Vue from 'vue'
import axios from 'axios'
import uuid from "node-uuid";
import qString from 'qs'

// axios.defaults.baseURL = 'http://pinkap.ldx178.com';
axios.defaults.baseURL = '/API/';
// axios.defaults.baseURL = 'http://localhost:4853';
Vue.prototype.$http = axios
Vue.prototype.HOST = ''
let ComFunJS = {
    admintoken: "FTSPTK_01",
    fileServer: '/fl-api/v2/tx2/fileupload',
    filePath: function (file, wh) {
        var url = "/fl-api/v2/tx2/file/" + file;
        if (wh) {
            url += "?resize=w_" + wh.splist('/')[0] + ",h_" + wh.split('/')[1];
        }
        return url;
    },
    getQueryString: function (name, defauval) {//获取URL参数,如果获取不到，返回默认值，如果没有默认值，返回空格
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) { return unescape(decodeURI(r[2])); }
        else {
            return defauval || "";
        }
    },
    changeUrlArg: function (url, arg, val) { //替换url参数值，如没有则新增该参数
        var pattern = arg + '=([^&]*)';
        var replaceText = arg + '=' + val;
        return url.match(pattern) ? url.replace(eval('/(' + arg + '=)([^&]*)/gi'), replaceText) : (url.match('[\?]') ? url + '&' + replaceText : url + '?' + replaceText);
    },
    setCookie: function (name, value, t) {  //设置cookie，默认保存7天
        var Days = 7;
        var exp = new Date();
        if (t) {
            exp.setTime(exp.getTime() + parseInt(t) * 24 * 60 * 60 * 1000);
        } else {
            exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
        }
        document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString() + ";path=/";
    },
    getCookie: function (name) {//获取cookie
        var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
        if (arr = document.cookie.match(reg))
            return unescape(arr[2]);
        else
            return null;
    },
    delCookie: function (name) { //删除cookie
        var exp = new Date();
        exp.setTime(exp.getTime() - 1);
        var cval = ComFunJS.getCookie(name);
        if (cval != null) {
            document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString() + ";path=/;";
        }
    },
    getToken: function () {
        var token = ComFunJS.getCookie(ComFunJS.admintoken)
        return token == null ? '' : token;
    },
    saveToken: function (token) {
        ComFunJS.setCookie(ComFunJS.admintoken, 'FTSPTK ' + token)
    },
    delToken: function () {
        ComFunJS.delCookie(ComFunJS.admintoken)
    },
    getNowDate: function (format, date) {
        if (!format) {
            format = "yyyy-MM-dd";
        }

        var now = new Date();
        if (date) {
            now = new Date(Date.parse(date.replace(/-/g, '/')));
        }

        var year = now.getFullYear(); //年
        var month = now.getMonth() + 1; //月
        var day = now.getDate(); //日
        var hh = now.getHours();
        var mm = now.getMinutes();
        var ss = now.getSeconds();

        var clock = year + "-";

        switch (format) {
            case "yyyy-MM-dd":
                {
                    if (month < 10)
                        clock += "0";
                    clock += month + "-";
                    if (day < 10) {
                        clock += "0";
                    }
                    clock += day;
                }
                break;
            case "yyyy-MM":
                {
                    if (month < 10)
                        clock += "0";
                    clock += month;
                }
                break;
            case "yyyy-MM-dd HH:mm":
                {
                    if (month < 10)
                        clock += "0";
                    clock += month + "-";
                    if (day < 10) {
                        clock += "0";
                    }
                    clock += day + " ";

                    if (hh < 10)
                        clock += "0";
                    clock += hh + ":";
                    if (mm < 10)
                        clock += "0";
                    clock += mm + ":";
                    if (ss < 10)
                        clock += "0";
                    clock += ss;
                    clock = clock.substring(0, 16)
                }
                break;
            case "yyyy-MM-ddTHH:mm":
                {
                    if (month < 10)
                        clock += "0";
                    clock += month + "-";
                    if (day < 10) {
                        clock += "0";
                    }
                    clock += day + "T";

                    if (hh < 10)
                        clock += "0";
                    clock += hh + ":";
                    if (mm < 10)
                        clock += "0";
                    clock += mm + ":";
                    if (ss < 10)
                        clock += "0";
                    clock += ss;
                    clock = clock.substring(0, 16)
                }
                break;
            case "yyyy-MM-dd HH:mm:ss":
                {
                    if (month < 10)
                        clock += "0";
                    clock += month + "-";
                    if (day < 10) {
                        clock += "0";
                    }
                    clock += day + " ";

                    if (hh < 10)
                        clock += "0";
                    clock += hh + ":";
                    if (mm < 10)
                        clock += "0";
                    clock += mm + ":";
                    if (ss < 10)
                        clock += "0";
                    clock += ss;
                }
                break;
            case "yyyyMMddHHmmss":
                {
                    clock = year;
                    if (month < 10)
                        clock += "0";
                    clock += month;
                    if (day < 10) {
                        clock += "0";
                    }
                    clock += day;

                    if (hh < 10)
                        clock += "0";
                    clock += hh;
                    if (mm < 10)
                        clock += "0";
                    clock += mm;
                    if (ss < 10)
                        clock += "0";
                    clock += ss;
                }
                break;
            case "MMddHHmm":
                {
                    clock = "";
                    var nowdata = new Date();
                    var nowYear = nowdata.getFullYear();
                    var nowMonth = nowdata.getMonth() + 1;
                    var nowDay = nowdata.getDate();
                    var nowdatastr = nowYear + nowMonth + nowDay;
                    var datastr = year + month + day;
                    if (nowdatastr == datastr) {
                        clock += hh + ":";
                        clock += mm;
                    } else {
                        clock += month + "-";
                        clock += day + " ";
                        clock += hh + ":";
                        clock += mm;
                    }
                }
                break;
        }

        return clock;

    },
    //Get方式访问带验证的API
    getJSON: function (url, data, callback) {
        if (typeof (data) == "function") {
            callback = data;
            data = null;
        }
        Vue.prototype.$http.get(url, { params: data, headers: { 'Authorization': 'FTSPTK ' + ComFunJS.getToken() } })
            .then((response) => {
                if (response.data.error) {
                    Vue.prototype.$message({
                        type: 'error',
                        message: response.data.error
                    })
                } else {
                    return callback & callback.call(this, response.data);
                }
            }).catch(function (response) {
                Vue.prototype.$message({
                    type: 'error',
                    message: response.message
                })
            })
    },
    //Post方式访问带验证的API
    post: function (url, data, callback) {
        if (typeof (data) == "function") {
            callback = data;
            data = null;
        }
        const loading = Vue.prototype.$loading({
            lock: true,
            text: '玩命加载中...',
            spinner: 'el-loading-spinners',
            background: 'rgba(255, 255, 255, 0.5)'
        });
        Vue.prototype.$http.post(url, qString.stringify(data), {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            }
        })
            .then((response) => {
                loading.close()
                if (response.data.error) {
                    Vue.prototype.$message({
                        type: 'error',
                        message: response.data.error
                    })
                } else {
                    return callback & callback.call(this, response.data);
                }

            }).catch(function (response) {
                loading.close()
                Vue.prototype.$message({
                    type: 'error',
                    message: response.message
                })
            })
    },
    getUUID() {
        return uuid.v1();
    }

};


Vue.prototype.$ComFunJS = ComFunJS
